<template>
  <div class="main-content" style="width: 50%">
    <img :src="notebook.cover" alt="" style="width: 100%; margin-top: 20px; border-radius: 5px">
    <div style="margin: 20px 0; font-size: 22px; font-weight: bold">{{ notebook.name }}</div>
    <div style="display: flex; margin-bottom: 30px">
      <div style="display: flex; align-items: center; ">
        <img :src="notebook.avatar" alt="" style="width: 30px; height: 30px; border-radius: 50%">
        <span style="margin-left: 5px">{{ notebook.userName }}</span>
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: flex-end">
        <span style="font-size: 20px; color: orange; font-weight: bold; margin-right: 2px">{{ notebook.count }}</span>
        浏览
        <span style="font-size: 20px; color: orange; font-weight: bold; margin-right: 2px; margin-left: 30px">{{
            notebook.collectCount
          }}</span> 收藏
        <span style="font-size: 20px; color: orangered; font-weight: bold; margin-right: 2px; margin-left: 30px">{{
            notebook.likesCount
          }}</span> 点赞

        <el-button icon="el-icon-like" style="margin-left: 10px; color: orange" v-if="notebook.userLike" @click="like">
          已点赞
        </el-button>
        <el-button icon="el-icon-unlike" style="margin-left: 10px" v-else @click="like">点赞</el-button>

        <el-button icon="el-icon-star-on" style="margin-left: 10px; color: orange" v-if="notebook.userCollect"
                   @click="collect">已收藏
        </el-button>
        <el-button icon="el-icon-star-off" style="margin-left: 10px" v-else @click="collect">收藏</el-button>

        <el-button icon="el-icon-warning-outline" style="margin-left: 10px; color: orange" v-if="notebook.userReport"
                   @click="handleReportExist">已举报
        </el-button>
        <el-button icon="el-icon-warning" style="margin-left: 10px" v-else @click="openReportDialog">举报</el-button>
      </div>
    </div>

    <div class="w-e-text" style="line-height: 24px; margin-bottom: 30px">
      <div style="margin: 20px 0; font-size: 22px; font-weight: bold">{{ notebook.title }}</div>
      <div v-html="notebook.content"></div>
    </div>

    <div style="margin: 30px 0; color: #666; font-size: 12px">发布时间:{{ notebook.releaseTime }}</div>
    <div style="margin: 30px 0; color: #666; font-size: 12px">上一次更新时间:{{ notebook.lastUpdateTime }}</div>
    <div style="margin: 30px 0" v-if="tagList&&tagList.length">
      <el-tag style="margin-right: 10px; font-family: 'Microsoft YaHei';cursor:pointer;font-size: 12px"
              v-for="tag in tagList" :key="tag" @click="handleTag(tag)"># {{ tag }}
      </el-tag>
    </div>

    <div style="margin: 50px 0">
      <Comment :fid="id" module="交流笔记"/>
    </div>

    <el-dialog :visible.sync="dialogFormVisible" title="举报" width="30%">
      <el-form :model="reportForm" :rules="rules" ref="formRef" label-width="80px" style="padding: 0 20px" status-icon>
        <el-form-item label="举报内容" prop="content">
          <el-input type="textarea" placeholder="请输入举报内容" v-model="reportForm.content"
                    autocomplete="off"></el-input>
        </el-form-item>
        <span style="display: flex;justify-content: center;margin-top: 10px">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="report">
          举报
        </el-button>
      </span>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import '@/assets/css/iconfont/iconfont.css'
import Comment from "@/components/Comment";

export default {
  name: "NotebookDetail",
  components: {Comment},
  data() {
    return {
      id: this.$route.query.id,  // 笔记ID
      notebook: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      dialogFormVisible: false,
      tagList: [],
      reportForm: {},
      rules: {
        content: [
          {required: true, message: '请输入举报内容', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.updateCount()
  },
  methods: {
    like() {  // 点赞
      this.$request.post('/likes/set', {fid: this.id, userId: this.user.id, module: '交流笔记'}).then(res => {
        if (res.code === '200') {
          this.$message.success('点赞成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    collect() {
      this.$request.post('/collect/set', {fid: this.id, userId: this.user.id, module: '交流笔记'}).then(res => {
        if (res.code === '200') {
          this.$message.success('收藏成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleReportExist() {
      this.$message.warning('您已经举报过了')
    },
    report() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          this.$request.post('/report/set', {
            fid: this.id,
            userId: this.user.id,
            module: '交流笔记',
            content: this.reportForm.content
          }).then(res => {
                if (res.code === '200') {
                  this.$message.success('举报成功')
                  this.load()
                  this.dialogFormVisible = false
                } else {
                  this.$message.error(res.msg)
                }
              }
          )
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    openReportDialog() {
      this.dialogFormVisible = true;
    },
    updateCount() {
      this.$request.put('/notebook/updateCount/' + this.id).then(res => {
        this.load()
      })
    },
    load() {
      this.$request.get('/notebook/selectById/' + this.id).then(res => {
        this.notebook = res.data || {}
        this.tagList = JSON.parse(this.notebook?.tags || '[]')
      })
    },
    handleTag(searchKeyWord) {
      window.open('/front/notebook?searchKeyword=' + searchKeyWord)
    }
  }
}
</script>

<style scoped>

</style>